/**
 * 
 */
package org.log4jconfig.xml.editor.contentassist;

import org.eclipse.core.runtime.Status;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor;
import org.log4jconfig.xml.editor.Log4jEditorPlugin;
import org.log4jconfig.xml.editor.contentassist.delegate.AppenderContentAssistDelegatedProcessor;
import org.log4jconfig.xml.editor.contentassist.delegate.AppenderRefContentAssistDelegatedProcessor;
import org.log4jconfig.xml.editor.contentassist.delegate.LayoutContentAssistDelegatedProcessor;
import org.log4jconfig.xml.editor.contentassist.delegate.LevelContentAssistDelegatedProcessor;
import org.log4jconfig.xml.editor.contentassist.delegate.LoggerContentAssistDelegatedProcessor;
import org.log4jconfig.xml.editor.contentassist.delegate.ParamContentAssistDelegatedProcessor;
import org.log4jconfig.xml.editor.utils.AttributeInfoLookupUtils;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class DelegatingContentAssistProcessor extends XMLContentAssistProcessor {

	private final LoggerContentAssistDelegatedProcessor loggerContentAssistProcessor = new LoggerContentAssistDelegatedProcessor();

	private final IContentAssistDelegatedProcessor levelContentAssistProcessor = new LevelContentAssistDelegatedProcessor();

	private final IContentAssistDelegatedProcessor appenderContentAssistProcessor = new AppenderContentAssistDelegatedProcessor();

	private final IContentAssistDelegatedProcessor appenderRefContentAssistProcessor = new AppenderRefContentAssistDelegatedProcessor();

	private final IContentAssistDelegatedProcessor layoutContentAssistProcessor = new LayoutContentAssistDelegatedProcessor();
	
	private final IContentAssistDelegatedProcessor paramContentAssistProcessor = new ParamContentAssistDelegatedProcessor();
	
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor
	 * #addAttributeValueProposals
	 * (org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest)
	 */
	@Override
	protected void addAttributeValueProposals(
			ContentAssistRequest contentAssistRequest) {
		String elementName = contentAssistRequest.getNode().getNodeName();
		String attributeName = AttributeInfoLookupUtils
				.getAttributeName(contentAssistRequest);
		String attributeMatchValue = AttributeInfoLookupUtils.getAttributeMatchValue(contentAssistRequest);
		String attributeCurrentValue = AttributeInfoLookupUtils.getAttributeCurrentValue(contentAssistRequest);
		try {
			IContentAssistDelegatedProcessor contentAssistDelegatedProcessor = null;
			if (elementName.equals("logger")) {
				contentAssistDelegatedProcessor = loggerContentAssistProcessor;
			} else if (elementName.equals("level")) {
				contentAssistDelegatedProcessor = levelContentAssistProcessor;
			} else if (elementName.equals("appender")) {
				contentAssistDelegatedProcessor = appenderContentAssistProcessor;
			} else if (elementName.equals("appender-ref")) {
				contentAssistDelegatedProcessor = appenderRefContentAssistProcessor;
			} else if (elementName.equals("layout")) {
				contentAssistDelegatedProcessor = layoutContentAssistProcessor;
			} else if (elementName.equals("param")) {
				contentAssistDelegatedProcessor = paramContentAssistProcessor;
			} else {
				Log4jEditorPlugin.getDefault().getLog().log(new Status(Status.WARNING,
						Log4jEditorPlugin.PLUGIN_ID, "Element name not (yet) supported for completion proposal: " +
						elementName));
			}
			if (contentAssistDelegatedProcessor != null) {
				contentAssistDelegatedProcessor.addAttributeValueProposals(
						contentAssistRequest, attributeName,
						attributeCurrentValue, attributeMatchValue);
			}
		} catch (Throwable e) {
			Log4jEditorPlugin.getDefault().getLog().log(
					new Status(Status.ERROR, Log4jEditorPlugin.PLUGIN_ID,
							"Error while computing content assist for element "
									+ elementName + ": " + e.getMessage(), e));

		}
		super.addAttributeValueProposals(contentAssistRequest);
	}

}
